package action;

import controller.Action;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Professor;
import model.Turma;

public class PesquisarNotaFrequenciaAction extends Action {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        String acao = request.getParameter("acao");
        try {
            request.setAttribute("professores", Professor.obterProfessores());
            request.setAttribute("acao", acao);

            if (!acao.equals("prepararOperacao")) {
                int matricula = Integer.parseInt(request.getParameter("optProfessor"));
                List<Turma> turmas = Turma.obterTurmas();
                List<Turma> turmaExcluir = new ArrayList<Turma>();

                Iterator turmaIterator = turmas.iterator();
                while(turmaIterator.hasNext()){
                    Turma turma = (Turma) turmaIterator.next();
                    if (turma.getProfessor().getMatricula() != matricula) {
                        turmaExcluir.add(turma);
                    }
                }

                turmas.removeAll(turmaExcluir);

                request.setAttribute("turmas", turmas);
                request.setAttribute("matricula", matricula);
            }

            RequestDispatcher janela = request.getRequestDispatcher("/PesquisarNotaFrequencia.jsp");
            janela.forward(request, response);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public void prepararOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void confirmarOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
